package com.onpositive.datamap.item;

import java.io.Serializable;
import java.util.HashMap;

import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.diskstorage.AbstractValueAccessor;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.core.IDataObject;
import com.onpositive.datamap.core.impl.AbstractDataProperty;
import com.onpositive.datamap.core.impl.SimpleProperty;
import com.onpositive.datamap.values.impl.ValueSequence;

public class MapDataAccessor extends AbstractValueAccessor implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected HashMap<String, IDataPropertyValue>values=new HashMap<String,IDataPropertyValue>();
	
	@Override
	public IDataPropertyValue getValue(IDataObject object,
			AbstractDataProperty p) {
		return (IDataPropertyValue) values.get(p.getId());
	}

	@Override
	public void setValue(IDataObject obj, AbstractDataProperty p, Object value) {
		if (value instanceof String&&p instanceof SimpleProperty){
			value=new ValueSequence(p, null, new String[]{(String) value});
		}
		if (value instanceof String[]&&p instanceof SimpleProperty){
			value=new ValueSequence(p, null,(String[]) value);
		}
		if (value instanceof Number){
			value=p.adaptFilterValue(value.toString());
		}
		IDataPropertyValue value2 = (IDataPropertyValue) value;
		if (value2!=null){
		value2=value2.clone(this);
		}
		values.put(p.getId(), value2);
	}

	@Override
	public void updateMeta(IPropertyMetadata mq) {
		
	}

}
